package vip.xiaonuo.smzq.modular.xxgs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJLambdaQueryWrapper;
import vip.xiaonuo.auth.core.pojo.SaBaseLoginUser;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.smzq.modular.deom.ToolData;
import vip.xiaonuo.smzq.modular.dictionary.entity.Cb08Xzqh;
import vip.xiaonuo.smzq.modular.dictionary.mapper.Cb08XzqhMapper;
import vip.xiaonuo.smzq.modular.kcl.mapper.SjyjMapper;
import vip.xiaonuo.smzq.modular.xxgs.entity.Xxgssj;
import vip.xiaonuo.smzq.modular.xxgs.entity.param.XxgssjParam;
import vip.xiaonuo.smzq.modular.xxgs.entity.vo.XxgssjVo;
import vip.xiaonuo.smzq.modular.xxgs.entity.vo.*;
import vip.xiaonuo.smzq.modular.xxgs.mapper.XxgssjMapper;
import vip.xiaonuo.smzq.modular.xxgs.service.IXxgssjService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.page.CommonPageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 信息公示_信息公示数据 服务实现类
 *
 * @author sqq
 * @since 2024-07-19
 */
@Service
public class XxgssjServiceImpl extends ServiceImpl<XxgssjMapper, Xxgssj> implements IXxgssjService {

    @Resource
    private XxgssjMapper xxgssjMapper;
    // 行政区划
    @Resource
    private Cb08XzqhMapper cb08XzqhMapper;
    //数据预警
    @Resource
    private SjyjMapper sjyjMapper;

    /**
     * 信息公示_信息公示数据分页查询
     */
    @Override
    public Page<XxgssjVo> page(XxgssjParam param) {

        IPage<XxgssjVo> iPage = new Page<>();
        iPage.setCurrent(param.getCurrent());
        iPage.setSize(param.getSize());

        LambdaQueryWrapper<Xxgssj> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(param.getNd()), Xxgssj::getLrrq, param.getNd());
        queryWrapper.like(StringUtils.isNotBlank(param.getKyqrmc()), Xxgssj::getKyqrmc, param.getKyqrmc());
        queryWrapper.eq(Xxgssj::getDelflag,0);
        return xxgssjMapper.getPageList(iPage, queryWrapper);
    }


    @Override
    public Page<Xxgssj> page(Xxgssj xxgssj) {
        return null;
    }

    /**
     * 市级驾驶舱——信息公示
     * @param nd 年度
     */
    @Override
    public List<JscXxgsVo> sjscXxgsList(String nd) {
        List<JscXxgsVo> jscXxgsVoList =new ArrayList<>();
        // 获取用户信息
        SaBaseLoginUser saBaseLoginUser = StpLoginUserUtil.getLoginUser();

       if (nd != null){
         if (saBaseLoginUser.getOrgId()!=null&&!saBaseLoginUser.getOrgId().equals("")){
             List<Cb08Xzqh> cb08XzqhList = cb08XzqhMapper.selectList(new QueryWrapper<Cb08Xzqh>().eq("fdm",Integer.valueOf(saBaseLoginUser.getOrgId())).orderByAsc("dm"));
             if (cb08XzqhList.size()>0){

                 for (Cb08Xzqh cb08Xzqh : cb08XzqhList){
                     JscXxgsVo jscXxgsVo = new JscXxgsVo();
                     jscXxgsVo.setXzqhdm(String.valueOf(cb08Xzqh.getDm()));
                     jscXxgsVo.setXzqhmc(cb08Xzqh.getMc());
                     // 异常名单数量
                    int ycmdCount = xxgssjMapper.getycmdCount(String.valueOf(cb08Xzqh.getDm()),nd);
                     //失信名单数量
                    int sxmdCount = xxgssjMapper.getsxmdCount(String.valueOf(cb08Xzqh.getDm()),nd);
                     //绿色矿山数量
                     int lskcsCount = xxgssjMapper.getlskcsCount(String.valueOf(cb08Xzqh.getDm()),nd);

                     jscXxgsVo.setLskcs(lskcsCount);
                     jscXxgsVo.setLrsxmds(sxmdCount);
                     jscXxgsVo.setLrycmds(ycmdCount);
                     jscXxgsVoList.add(jscXxgsVo);

                 }
             }
         }
       }
        return jscXxgsVoList;
    }

    /**
     * 市级驾驶舱——矿山数据预警
     * @param nd 年度
     */
    @Override
    public List<SjscKssjyjVo> kssjyjList(String nd) {
        List<SjscKssjyjVo> sjscKssjyjVoList =new ArrayList<>();

        // 获取用户信息
        SaBaseLoginUser saBaseLoginUser = StpLoginUserUtil.getLoginUser();

        if (nd != null) {
            if (saBaseLoginUser.getOrgId() != null && !saBaseLoginUser.getOrgId().equals("")) {
                List<Cb08Xzqh> cb08XzqhList = cb08XzqhMapper.selectList(new QueryWrapper<Cb08Xzqh>().eq("fdm", Integer.valueOf(saBaseLoginUser.getOrgId())).orderByAsc("dm"));
                if (cb08XzqhList.size() > 0) {
                    for (Cb08Xzqh cb08Xzqh : cb08XzqhList) {
                        SjscKssjyjVo jscXxgsVo = new SjscKssjyjVo();
                        jscXxgsVo.setXzqdm(String.valueOf(cb08Xzqh.getDm()));
                        jscXxgsVo.setXzqhmc(cb08Xzqh.getMc());
                        //储量预警总数
                        jscXxgsVo.setClyj(sjyjMapper.getCount(" xzqhdm='"+cb08Xzqh.getDm() +"' and nd="+nd+" and sjyjlxbm="+"308004"));
                        //查明预警总数
                        jscXxgsVo.setCmyj(sjyjMapper.getCount(" xzqhdm='"+cb08Xzqh.getDm() +"' and nd="+nd+" and sjyjlxbm="+"308005"));
                        //保有预警总数
                        jscXxgsVo.setByyj(sjyjMapper.getCount(" xzqhdm='"+cb08Xzqh.getDm() +"' and nd="+nd+" and sjyjlxbm="+"308003"));
                        sjscKssjyjVoList.add(jscXxgsVo);

                    }
                }
            }
        }
        return sjscKssjyjVoList;
    }



    /**
     * 市级驾驶舱——资源储量统计
     * @param nd 年度
     * @param kcdm 矿山代码
     */
    @Override
    public KcZyltjVo shiZyclTj(String nd, String kcdm) {
        KcZyltjVo kcZyltjVo = new KcZyltjVo();

        // 获取用户信息
        SaBaseLoginUser saBaseLoginUser = StpLoginUserUtil.getLoginUser();
        List<ZycltjVo> zyltjVoList=new ArrayList<>();
        List<ZycltjVo> cltjVoList=new ArrayList<>();
        if (nd != null) {
            if (saBaseLoginUser.getOrgId() != null && !saBaseLoginUser.getOrgId().equals("")) {
                List<Cb08Xzqh> cb08XzqhList = cb08XzqhMapper.selectList(new QueryWrapper<Cb08Xzqh>().eq("fdm", Integer.valueOf(saBaseLoginUser.getOrgId())).orderByAsc("dm"));

                if (cb08XzqhList.size() > 0) {
                    for (Cb08Xzqh cb08Xzqh : cb08XzqhList) {
                        ZycltjVo zyltjVo = new ZycltjVo();
                        ZycltjVo cltjVo = new ZycltjVo();
                        JscZyclTj jscZyclTj =xxgssjMapper.getZycltj(" hz.nd="+nd+" and hz.xzqdm='"+cb08Xzqh.getDm()+"' and hz.kcdm="+kcdm);
                        zyltjVo.setXzqdm(String.valueOf(cb08Xzqh.getDm()));
                        zyltjVo.setXzqhmc(cb08Xzqh.getMc());
                        cltjVo.setXzqdm(String.valueOf(cb08Xzqh.getDm()));
                        cltjVo.setXzqhmc(cb08Xzqh.getMc());
                        if(jscZyclTj!=null){
                            if (jscZyclTj.getZyl()!=null){
                                zyltjVo.setTjl(ToolData.roundToTwoDecimals(jscZyclTj.getZyl()));
                            }else{
                                zyltjVo.setTjl(0.00);
                            }

                            if (jscZyclTj.getCl()!=null){
                                cltjVo.setTjl(ToolData.roundToTwoDecimals(jscZyclTj.getCl()));
                            }else{
                                cltjVo.setTjl(0.00);
                            }
                        }else{
                            zyltjVo.setTjl(0.00);
                            cltjVo.setTjl(0.00);
                        }



                        zyltjVoList.add(zyltjVo);
                        cltjVoList.add(cltjVo);

                    }
                }
            }
        }
        kcZyltjVo.setZyltjVoList(zyltjVoList);
        kcZyltjVo.setCltjVoList(cltjVoList);
        return kcZyltjVo;
    }
}
